package view;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import model.dessin;
import model.freeman;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;

import view.MarkPen.DrawPanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class Interface extends JFrame {
	
	dessin objDessin=new dessin();
	
String code=""; 
	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	//-16777216 black
	//-1        white

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Interface frame = new Interface();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Interface() {
		setTitle("Projet Apprentissage");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 264, 177);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		final DrawPanel pan=new DrawPanel();
		pan.setBackground(Color.WHITE);
		pan.setBounds(148, 31, 50, 50);		
		pan.addMouseListener(new MouseAdapter()
		{
			public void mousePressed(MouseEvent e)
			{
				//objDessin.dessiner(e,20);
			}
			
		}
		);
		
		contentPane.add(pan);
		
		JButton btnMatricePixel = new JButton("Matrice pixel");
		btnMatricePixel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
				
				// Paint the JPanel to a BufferedImage.
				Dimension size = pan.getSize();
				int imageType = BufferedImage.TYPE_INT_ARGB;
				BufferedImage image = new BufferedImage(size.width, size.height, imageType);
				Graphics2D g2d = image.createGraphics();
				pan.paint(g2d);
				
				for (int y=0; y<size.height; y++) {
				  for (int x=0; x<size.width; x++) {
				    int pixel = image.getRGB(x, y);
				    
				    if (pixel == 0xFF000000) {
				    	code=code+"1";
				    } 
				    else if (pixel == 0xFFFFFFFF) {
				    	code=code+"0";
				    }
				    else {
				    System.out.println("autre");
				    }
				  }
				}
				
				try {
					
					dessin.ajouterCode(code);
					String contour=freeman.freeman(code);
					if(contour.length()>0)
					{
					System.out.println("Contour: "+contour);
					System.out.println("Taille contour: "+contour.length());
					int solution=DetecterCaractere.detecterCar(contour);
					System.out.println("le chiffre est: "+solution);
					code="";
					}
					else
					{
						System.out.println("Retracer votre chiffre SVP");
						System.out.println("Taille contour: "+contour.length());
						code="";
					}
					
					
				} 
				catch (IOException e1) {
					e1.printStackTrace();
				}
				
				
			}
		});
		btnMatricePixel.setBounds(17, 55, 117, 29);
		contentPane.add(btnMatricePixel);
	}	
}
